package de.fhhannover.inform.itteam.snmpnetviz.gui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

import de.fhhannover.inform.itteam.snmpnetviz.gui.util.ScaleImage;

public class ControlPanel extends JPanel implements ActionListener, MouseListener {
	private static final long serialVersionUID = -5492479617942535207L;
	private JButton toggleAnimation, toggleKey;
	private NVFrame frame;
	private KeyPanel keyPanel;
	private boolean animated, flag_key_visible;
	private int sizeX, sizeY;
	
	//
	private final ImageIcon animation_active = new ImageIcon(this.getClass().getResource("/gfx/snmpNetViz/animation_active.png"));
	private final ImageIcon animation_disabled = new ImageIcon(this.getClass().getResource("/gfx/snmpNetViz/animation_inactive.png"));
	private final ImageIcon key_visible = new ImageIcon(this.getClass().getResource("/gfx/snmpNetViz/key_active.png"));
	private final ImageIcon key_invisible = new ImageIcon(this.getClass().getResource("/gfx/snmpNetViz/key_inactive.png"));
	// for mouse events
	private final ImageIcon animation_hover = new ImageIcon(this.getClass().getResource("/gfx/snmpNetViz/animation_hover.png"));
	private final ImageIcon key_hover = new ImageIcon(this.getClass().getResource("/gfx/snmpNetViz/key_hover.png"));
	
	public ControlPanel(NVFrame frame, KeyPanel panel) {		
		FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
		this.setLayout(flow);
		this.setOpaque(false);
		
		this.frame = frame;
		this.keyPanel = panel;
		
		animated = true;
		flag_key_visible = true;
		
		sizeX = Math.round(frame.getWidth() / 14);
		sizeY = Math.round(sizeX * 1.02f);
		
		this.setPreferredSize(new Dimension(
				Math.round(sizeX*2.1f), Math.round(sizeY*1.1f)));
				
		// create buttons
		toggleAnimation = new JButton(ScaleImage.resize(sizeX, sizeY, animation_active));
		toggleKey = new JButton(ScaleImage.resize(sizeX, sizeY, key_visible));
		
		// configure buttons
		toggleAnimation.setPreferredSize(new Dimension(sizeX, sizeY));
		toggleAnimation.setBorder(null);
		toggleAnimation.setContentAreaFilled(false);
		toggleAnimation.addActionListener(this);
		toggleAnimation.addMouseListener(this);
		toggleKey.setPreferredSize(new Dimension(sizeX, sizeY));
		toggleKey.setBorder(null);
		toggleKey.setContentAreaFilled(false);
		toggleKey.addActionListener(this);
		toggleKey.addMouseListener(this);
		
		// add buttons
		this.add(toggleAnimation);
		this.add(toggleKey);
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		// toggle between animated and still
		if (ae.getSource() == toggleAnimation) {
			if (animated) {
				// TODO	Does this work (actions gets revalidated)?
				frame.getVisualization().getAction("layout").cancel();
				toggleAnimation.setIcon(ScaleImage.resize(sizeX, sizeY, animation_disabled));
				animated = false;
			} else {
				frame.getVisualization().getAction("layout").run();
				toggleAnimation.setIcon(ScaleImage.resize(sizeX, sizeY, animation_active));
				animated = true;
			}
		}
		
		// (de)activate key for additional information
		if (ae.getSource() == toggleKey) {
			if (flag_key_visible) {
				keyPanel.setVisible(false);
				flag_key_visible = false;
			} else {
				keyPanel.setVisible(true);
				flag_key_visible = true;
			}
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		if (e.getSource() == toggleAnimation) 
			toggleAnimation.setIcon(ScaleImage.resize(sizeX, sizeY, animation_hover));
		if (e.getSource() == toggleKey)
			toggleKey.setIcon(ScaleImage.resize(sizeX, sizeY, key_hover));
	}

	@Override
	public void mouseExited(MouseEvent e) {
		if (e.getSource() == toggleAnimation) {
			if (animated)
				toggleAnimation.setIcon(ScaleImage.resize(sizeX, sizeY, animation_active));
			else 
				toggleAnimation.setIcon(ScaleImage.resize(sizeX, sizeY, animation_disabled));
		}
		
		if (e.getSource() == toggleKey) {
			if (flag_key_visible)
				toggleKey.setIcon(ScaleImage.resize(sizeX, sizeY, key_visible));
			else 
				toggleKey.setIcon(ScaleImage.resize(sizeX, sizeY, key_invisible));
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

}
